package android.activities;

import android.app.ListActivity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ShowParkingActivity extends ListActivity {
	
	private String _jsonData;
	private ArrayList<HashMap<String, String>> mylist;
	private HashMap<String, String> map;
	private ListView lv_parkingspaces;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.showparking);
        
        mylist = new ArrayList<HashMap<String,String>>();
        
        Bundle bundle = getIntent().getExtras();
        _jsonData = bundle.getString("result");
        if (_jsonData != null) {
        	Log.i("result", _jsonData);
        	fillList(_jsonData);
        }
        else { Log.e("ERROR", "Error: Data is null -> " + _jsonData); }
	}
    
    public void fillList(String jsonData){
    	try {
			JSONArray jArray = new JSONArray(jsonData);
			
	        for(int i=0;i<jArray.length();i++) {
				map = new HashMap<String, String>();
                JSONObject json_data = jArray.getJSONObject(i);
               
                map.put("name", json_data.getString("parking_NAME"));
                map.put("code", json_data.getString("parking_CODE"));
                map.put("status", json_data.getString("status_DESCRIPTION"));
                map.put("lastupdated", json_data.getString("parking_LAST_UPDATED"));
                map.put("interval", json_data.getString("parking_INTERVAL"));
                map.put("geolocation", json_data.getString("parking_GEOLOCATION"));
                
                mylist.add(map);
	        }
		}
		catch(JSONException ex) { Log.e("ERROR", ex.toString()); }
		
		try {
			ListAdapter adapter = new SimpleAdapter(ShowParkingActivity.this, mylist, R.layout.listitemsetup, 
													new String[] { "name", "status", "lastupdated" }, 
													new int[] { R.id.txt_ParkingName, R.id.txt_ParkingStatus, R.id.txt_ParkingLastUpdated });
			setListAdapter(adapter);
			
			lv_parkingspaces = getListView();
			lv_parkingspaces.setTextFilterEnabled(true);
		
		}
		catch(Exception ex) { Log.e("ERROR", ex.toString()); }
    }
}
